package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.StudyPlan;
import com.xmy.cultivate.mapper.StudentLessonNumMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 学习课次表 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class StudentLessonNumServiceImpl extends ServiceImpl<StudentLessonNumMapper, StudentLessonNum> implements IStudentLessonNumService {

    @Autowired
    @Lazy
    ICourseSchedulingDetailService iCourseSchedulingDetailService;

    @Autowired
    @Lazy
    IGradeService iGradeService;

    @Autowired
    @Lazy
    IRecordCourseService iRecordCourseService;

    @Autowired
    StudentLessonNumMapper studentLessonNumMapper;

    @Autowired
    ICourseService iCourseService;

    @Autowired
    IChaStudyPlanService iChaStudyPlanService;

    @Autowired
    @Lazy
    IStudentGradeService iStudentGradeService;

    @Autowired
    @Lazy
    ITranscriptService iTranscriptService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    @Lazy
    IStudentClassTypeService iStudentClassTypeService;

    @Autowired
    @Lazy
    ICourseSchedulingAlwaysService iCourseSchedulingAlwaysService;

    @Override
    public void addStudentLesson(Long gradeId, Long studentId) {
        Grade grade = iGradeService.getById(gradeId);
        Course course = iCourseService.getById(grade.getCourseId());
        if (grade.getLessonType().equals(1) && course != null) {

            //log.info("=="+grade.getName());
            Integer yearPart = grade.getYearPart();
            Integer quarterNum = grade.getQuarterNum();

            QueryWrapper<CourseSchedulingDetail> courseSchedulingDetailQueryWrapper = new QueryWrapper<>();
            courseSchedulingDetailQueryWrapper.eq("year_part", yearPart);
            courseSchedulingDetailQueryWrapper.eq("quarter", quarterNum);
            //courseSchedulingDetailQueryWrapper.eq("lesson_type",)
            courseSchedulingDetailQueryWrapper.eq("grade_id", gradeId);
            courseSchedulingDetailQueryWrapper.orderByAsc("lesson_num");
            List<CourseSchedulingDetail> courseSchedulingDetailList = iCourseSchedulingDetailService.list(courseSchedulingDetailQueryWrapper);
            for (CourseSchedulingDetail courseSchedulingDetail : courseSchedulingDetailList) {
                QueryWrapper<RecordCourse> recordCourseQueryWrapper = new QueryWrapper<>();
                recordCourseQueryWrapper.eq("year_part", yearPart);
                recordCourseQueryWrapper.eq("quarter", quarterNum);
                recordCourseQueryWrapper.eq("student_id", studentId);
                recordCourseQueryWrapper.eq("subjects_id", course.getSubjectsId());
                recordCourseQueryWrapper.eq("lesson_type", grade.getLessonType());
                recordCourseQueryWrapper.eq("lesson_num", courseSchedulingDetail.getLessonNum());
                recordCourseQueryWrapper.last("limit 1");
                RecordCourse recordCourse = iRecordCourseService.getOne(recordCourseQueryWrapper);

                //课次
                StudentLessonNum studentLessonNum = null;
                QueryWrapper<StudentLessonNum> lessonNumQueryWrapper = new QueryWrapper<>();
                lessonNumQueryWrapper.eq("year_part", yearPart);
                lessonNumQueryWrapper.eq("quarter_num", quarterNum);
                lessonNumQueryWrapper.eq("subjects_id", course.getSubjectsId());
                lessonNumQueryWrapper.eq("student_id", studentId);
                lessonNumQueryWrapper.eq("lesson_num", courseSchedulingDetail.getLessonNum());
                lessonNumQueryWrapper.last("limit 1");
                studentLessonNum = studentLessonNumMapper.selectOne(lessonNumQueryWrapper);
                if (studentLessonNum == null) {
                    studentLessonNum = new StudentLessonNum();
                }

                studentLessonNum.setStudentId(studentId);
                studentLessonNum.setSubjectsId(course.getSubjectsId());
                studentLessonNum.setYearPart(yearPart);
                studentLessonNum.setQuarterNum(quarterNum);
                studentLessonNum.setLessonNum(courseSchedulingDetail.getLessonNum());
                if (recordCourse == null) {
                    studentLessonNum.setGradeId(courseSchedulingDetail.getGradeId());
                    studentLessonNum.setSchoolId(courseSchedulingDetail.getSchoolId());
                } else {
                    studentLessonNum.setGradeId(recordCourse.getGradeId());
                    studentLessonNum.setSchoolId(recordCourse.getSchoolId());
                }
                this.saveOrUpdate(studentLessonNum);
            }
        }
    }

    /**
     * 判断是否通关，已通关不算应到
     */
    @Override
    public void chaStudyPlanAdd(ChaStudyPlan studyPlan) {
        QueryWrapper<ChaStudyPlan> queryWrapper = new QueryWrapper<>();
        if (studyPlan.getStudentId() != null) {
            queryWrapper.eq("student_id", studyPlan.getStudentId());
        } else {
            return;
        }
        queryWrapper.eq("subjects_id", studyPlan.getSubjectsId());
        queryWrapper.eq("year_part", studyPlan.getYearPart());
        queryWrapper.eq("quarter_num", studyPlan.getQuarterNum());
        queryWrapper.eq("lesson_num", studyPlan.getLessonNum());
        queryWrapper.eq("lesson_type", studyPlan.getLessonType());
        queryWrapper.eq(studyPlan.getStatus()!=null,"status", studyPlan.getStatus());
        queryWrapper.eq(studyPlan.getCategoryId() != null,"category_id", studyPlan.getCategoryId());
        queryWrapper.last("limit 1");
        ChaStudyPlan chaStudyPlan = iChaStudyPlanService.getOne(queryWrapper);

        if (Objects.isNull(chaStudyPlan)) {
            if (studyPlan.getCreatedAt() == null){
                studyPlan.setCreatedAt(LocalDateTime.now().withNano(0));
            }
            iChaStudyPlanService.save(studyPlan);
        } else {
            if (!chaStudyPlan.getStatus().equals(1)){
                if (studyPlan.getCreatedAt()==null){
                    studyPlan.setCreatedAt(LocalDateTime.now().withNano(0));
                }
            }
            studyPlan.setId(chaStudyPlan.getId());
            studyPlan.setStatus(studyPlan.getStatus());
            iChaStudyPlanService.updateById(studyPlan);
        }
    }

}

